#lang racket

(define tolerance 0.00001)

(define (average x y) (/ (+ x y) 2))

(define (fixed-point f first-guess)
  (define (close-enough? v1 v2)
    (< (abs (- v1 v2)) tolerance))
  (define (try guess time)
    (let ((next (f guess)))
      (display time)
      (display ": ")
      (display guess)
      (newline)
      (if (close-enough? guess next)
          next
          (try next (+ time 1)))))
  (try first-guess 1))

(define (repeated f n)
  (define (iter i result)
    (if (= i n)
        result
        (iter (+ i 1) (compose f result))))
  (iter 1 f))

(define (average-damp f)
  (lambda (x) (average x (f x))))

(define (exp b n)
  (define (iter i result)
    (if (= i n)
        result
        (iter (+ i 1) (* b result))))
  (iter 1 b))

(define (n-root-fp x n) (lambda (y) (/ x (exp y (- n 1)))))

(define (n-root x n)
(fixed-point ((repeated average-damp (floor (/ (log n) (log 2)))) (n-root-fp x n)) 1.0))

(n-root 4294967296 32)
